package com.beiing.tianshuai.tianshuai.mine.ui;

import android.content.Context;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.flyco.tablayout.listener.CustomTabEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class MyPublishedActivitiesActivity extends BaseActivity {

    private Context mContext;
    private Unbinder mUnbinder;
    private String[] mTabTitles = {"全部", "审核中", "审核失败", "已结束"};
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @BindView(R.id.toolbar_navigation)
    ImageView mToolbarNavigation;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
//    @BindView(R.id.tab_layout)
//    CommonTabLayout mTabLayout;
//    @BindView(R.id.fragment_container)
//    FrameLayout mFragmentContainer;
    @BindView(R.id.tab_layout)
    TabLayout mTabLayout;
    @BindView(R.id.view_pager)
    ViewPager mViewPager;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_published_activities;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initListener();
        initTabLayout();
    }

    private void initListener() {
        // 返回
        mToolbarNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initTabLayout() {
        String uid = UserInfoBean.getUid(mContext);
        final ArrayList<Fragment> fragments = new ArrayList<>();
        fragments.add(ActivitiesPublishFragment.newInstance(uid, "全部"));
        fragments.add(ActivitiesPublishFragment.newInstance(uid, "审核中"));
        fragments.add(ActivitiesPublishFragment.newInstance(uid, "审核失败"));
        fragments.add(ActivitiesPublishFragment.newInstance(uid, "已结束"));
//        for (String tabTitle : mTabTitles) {
//            mTabEntities.add(new TabEntity(tabTitle, 0, 0));
//        }
//        mTabLayout.setTabData(mTabEntities, this, R.id.fragment_container, fragments);
//        mTabLayout.setCurrentTab(0);

        mViewPager.setOffscreenPageLimit(fragments.size());     //预加载
        mViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return mTabTitles[position];
            }
        });
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void onDestroy() {
        mUnbinder.unbind();
        super.onDestroy();
    }
}
